﻿using Admin.NET.Application.Entity;
using Furion.DatabaseAccessor;
using Furion.DatabaseAccessor.Extensions;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Furion.Extras.Admin.NET;
using Furion.FriendlyException;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace Admin.NET.Application
{
    /// <summary>
    /// Project服务
    /// </summary>
    [ApiDescriptionSettings("自己的业务", Name = "Project", Order = 100)]
    public class ProjectService : IProjectService, IDynamicApiController, ITransient
    {
        private readonly IRepository<Project,MasterDbContextLocator> _projectRep;

        public ProjectService(
            IRepository<Project,MasterDbContextLocator> projectRep
        )
        {
            _projectRep = projectRep;
        }

        /// <summary>
        /// 分页查询Project
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("/Project/page")]
        public async Task<PageResult<ProjectOutput>> Page([FromQuery] ProjectInput input)
        {
            var projects = await _projectRep.DetachedEntities
                                     .Where(!string.IsNullOrEmpty(input.ProjectName), u => u.ProjectName.ToLower().Contains( input.ProjectName.ToLower()))
                                     .Where(!string.IsNullOrEmpty(input.ProjectCategory), u => u.ProjectCategory.ToLower().Contains(input.ProjectCategory.ToLower()))
                                     .OrderBy(PageInputOrder.OrderBuilder<ProjectInput>(input))
                                     .ProjectToType<ProjectOutput>()
                                     .ToADPagedListAsync(input.PageNo, input.PageSize);

            return projects;
        }

        /// <summary>
        /// 增加Project
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("/Project/add")]
        public async Task Add(AddProjectInput input)
        {
            var project = input.Adapt<Project>();
            project.ProjectId= Guid.NewGuid();
            await _projectRep.InsertAsync(project);
        }

        /// <summary>
        /// 删除Project
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("/Project/delete")]
        public async Task Delete(DeleteProjectInput input)
        {
            var project = await _projectRep.FirstOrDefaultAsync(u => u.Id == input.Id);
            await _projectRep.DeleteAsync(project);
        }

        /// <summary>
        /// 更新Project
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("/Project/edit")]
        public async Task Update(UpdateProjectInput input)
        {
            var isExist = await _projectRep.AnyAsync(u => u.Id == input.Id, false);
            if (!isExist) throw Oops.Oh(ErrorCode.D3000);

            var project = input.Adapt<Project>();
            await _projectRep.UpdateAsync(project,ignoreNullValues:true);
        }

        /// <summary>
        /// 获取Project
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("/Project/detail")]
        public async Task<ProjectOutput> Get([FromQuery] QueryeProjectInput input)
        {
            return (await _projectRep.DetachedEntities.FirstOrDefaultAsync(u => u.Id == input.Id)).Adapt<ProjectOutput>();
        }

        /// <summary>
        /// 获取Project列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("/Project/list")]
        public async Task<List<ProjectOutput>> List([FromQuery] ProjectInput input)
        {
            return await _projectRep.DetachedEntities.ProjectToType<ProjectOutput>().ToListAsync();
        }    

    }
}
